function TLSetCtrlsStatus(dlgfig, ctrlsStruct, ctrlsList)

%TLSETCTRLSSTATUS Summary of this function goes here
%
%   TLSETCTRLSSTATUS(varagin) Detailed explanation goes here
%
%   Inputs:
%
%
%   Outputs:
%
%
%   Syntax:
%
%
%   Example:
%
%
%   See also:
%
% 
%   Author(s): Xiaobiao Huang
%   Copyright 2013-2020 The Xiaobiao Studio.
%   $Revision: 1.0.0.0 $  $Date: 05-Jul-2013 22:04:18 $

% TL_SET_CTRL_STATUS sets control status of controls.
%
% SYNTAX tl_set_ctrls_status(dlgfig, ctrlsStruct, ctrlsList)
%   sets control status of controls
%
%   INPUT ARGUMENTS
%   dlgfig          figure handle
%   ctrlsStruct     list of all controls
%   ctrlsList       list of controls to be set
%
%   OUTPUT ARGUMENTS
%   - / -


% Author(s): D. Andoleit
% $RCSfile: tl_set_ctrls_status.m $
% $ProjectName: e:/ARC/Components/HostSW/SimulinkBlocksets/TLBlocksets/CommonBlockset/CommonBlockset_GeneralUI/Sources/MSrc/project.pj $
% $Revision: 1.10 $
% $Date: 2011/08/25 12:46:22MESZ $
%
% Copyright ?2011 dSPACE GmbH. All rights reserved.

if dlgfig ~= 1
  dlgfig = get(dlgfig,'Parent');
end
disableBgColor = get(dlgfig,'Color');
enableBgColor = 'w';
errorFgColor = 'r';

for ctrlsNo = 1:length(ctrlsList)
    if isfield(ctrlsStruct, ctrlsList{ctrlsNo})
        % control exists: get user data control status and style of control
        ctrlHandle = ctrlsStruct.(ctrlsList{ctrlsNo});
        userData = get(ctrlHandle,'UserData');
        controlUserData = userData.ctrlStatus;
        if strcmp(get(ctrlHandle, 'Type'), 'uipanel')
            continue;
        end
        
        % uicontrol
        ctrlStyle = get(ctrlHandle,'Style');

        % set background, string, value, etc according to enable status (depending on the style of
        % the control)
        switch ctrlStyle

            case 'edit'
                % save enable status for label
                lableEnable = controlUserData.enable;
                % set bgcolor and string according to enable status
                if strcmp(controlUserData.enable, 'off')
                    controlUserData.string = '';
                    controlUserData.bgcolor = disableBgColor;
                elseif strcmp(controlUserData.enable, 'inactive')
                    controlUserData.enable = 'off';
                    controlUserData.bgcolor = enableBgColor;
                else
                    controlUserData.bgcolor = enableBgColor;
                end
                % cut string at the left-hand end, if string exceeds edit field width 
                pos = get(ctrlHandle, 'Position');
                commonGeom = ds_get_common_geometry;
                textExtent = commonGeom.textExtent;
                set(textExtent, 'String', controlUserData.string);
                ext = get(textExtent, 'Extent');
                bExt = false;
                while ext(3) > pos(3)
                    controlUserData.string = controlUserData.string(2:end);
                    set(textExtent, 'String', controlUserData.string);
                    ext = get(textExtent, 'Extent');
                    bExt = true;
                end
                if bExt && strcmp(controlUserData.enable, 'on')
                    controlUserData.enable = 'inactive';
                    buttondownFcn = @activateEdit;
                else
                    buttondownFcn = '';
                end
                
                % set control data
                % first clear background color to prevent wrong color in dialog
                % set non empty string, because foregroundcolor can only be set on a non empty
                % string, and afterwards sets the correct (empty) string
                set(ctrlHandle, ...
                    'BackGroundColor', 'w', ...
                    'Enable', controlUserData.enable, ...
                    'String', ' ', ...
                    'ButtonDownFcn', buttondownFcn, ...
                    'BackGroundColor', controlUserData.bgcolor);
                % set foregroundcolor according to error status
                if ~isfield(controlUserData, 'iserror') || ~controlUserData.iserror
                    set(ctrlHandle, 'ForeGroundColor', [0 0 0]);
                elseif controlUserData.iserror
                    set(ctrlHandle, 'ForeGroundColor', errorFgColor);
                end
                set(ctrlHandle, 'String', controlUserData.string);
                
                % set status of label according to edit status
                labelCtrl = [ctrlsList{ctrlsNo}(1:end-4), 'Label'];
                if isfield(ctrlsStruct, labelCtrl)
                    % there are edit controls without labels (e.g. in the
                    % look-up table(2d) dialog)
                    ctrlHandleLabel = ctrlsStruct.(labelCtrl);
                    set(ctrlHandleLabel, 'Enable', lableEnable);
                end

            case 'checkbox'
                % it is a checkbox field:
                if strcmp(controlUserData.enable, 'off')
                    % value is zero
                    controlUserData.value = 0;
                elseif strcmp(controlUserData.enable, 'inactive')
                    % delete these lines if checkbox is only one control (and not checkbox + label)
                    controlUserData.enable = 'off';
                    %controlUserData.value = 0;
                end
                set(ctrlHandle, ...
                    'Enable', controlUserData.enable);
                if ~isfield(controlUserData,'value')
                    controlUserData.value = 0;
                end
                set(ctrlHandle, 'Value',  controlUserData.value);
                if isfield(controlUserData, 'string')
                    set(ctrlHandle, 'String', controlUserData.string);
                end
                
            case 'popupmenu'
                % save enable status for label
                lableEnable = controlUserData.enable;
                % set bgcolor and string according to enable status
                if strcmp(controlUserData.enable, 'off')
                    controlUserData.string = ' ';
                    controlUserData.value  = 1;
                    controlUserData.bgcolor = disableBgColor;
                elseif strcmp(controlUserData.enable, 'inactive')
                    controlUserData.enable = 'off';
                    controlUserData.bgcolor = enableBgColor;
                else
                    controlUserData.bgcolor = enableBgColor;
                end
                set(ctrlHandle, ...
                    'Enable', controlUserData.enable, ...
                    'String', controlUserData.string, ...
                    'Value', controlUserData.value, ...
                    'BackGroundColor', controlUserData.bgcolor);
                % set foregroundcolor according to error status
                if ~isfield(controlUserData, 'iserror')
                    set(ctrlHandle, 'ForeGroundColor', [0 0 0]);
                elseif controlUserData.iserror
                    set(ctrlHandle, 'ForeGroundColor', errorFgColor);
                else
                    set(ctrlHandle, 'ForeGroundColor', [0 0 0]);
                end
                % set status of label according to popup status
                labelCtrl = [ctrlsList{ctrlsNo}(1:end-5), 'Label'];
                if isfield(ctrlsStruct, labelCtrl)
                    ctrlHandle = ctrlsStruct.(labelCtrl);
                    set(ctrlHandle, 'Enable', lableEnable);
                end

            otherwise % text or pushbutton or togglebutton
                set(ctrlHandle, 'Enable', controlUserData.enable);
                if isfield(userData, 'cDataEnabled')
                    if strcmp(controlUserData.enable, 'on')
                        set(ctrlHandle,'CData', userData.cDataEnabled);
                    else
                        set(ctrlHandle,'CData', userData.cDataDisabled);
                    end
                end
                if isfield(controlUserData,'string')
                    set(ctrlHandle, 'String', controlUserData.string);
                end
                if isfield(controlUserData,'bgcolor')
                    set(ctrlHandle,'BackGroundColor', controlUserData.bgcolor);
                end
                if isfield(controlUserData,'value')
                    if strcmp(controlUserData.enable, 'off')
                        controlUserData.value = 0;
                    end
                    set(ctrlHandle, 'Value',  controlUserData.value);
                end
                if ~isfield(controlUserData, 'iserror')
                    set(ctrlHandle, 'ForeGroundColor', [0 0 0]);
                elseif controlUserData.iserror
                    set(ctrlHandle, 'ForeGroundColor', errorFgColor);
                else
                    set(ctrlHandle, 'ForeGroundColor', [0 0 0]);
                end

                % set status of label according to control status
                labelCtrl = [ctrlsList{ctrlsNo}(1:end-4), 'Label'];
                if isfield(ctrlsStruct, labelCtrl)
                    % i.e. text control has label
                    ctrlHandle = ctrlsStruct.(labelCtrl);
                    set(ctrlHandle, 'Enable', controlUserData.enable);
                end
        end
        
        % set status of context menu according to associated control status
        cMenu = get(ctrlHandle, 'UIContextMenu');
        if ~isempty(cMenu)
            menu = get(cMenu, 'UserData');
            fields = fieldnames(menu);
            for nofields = 1 : length(fields)
                menuCtrl = menu.(fields{nofields});
                if strcmp(controlUserData.enable, 'off')
                    % special case: if status of associated control is off, menu is invisible
                    set(menuCtrl, 'Visible', controlUserData.enable);
                else
                    % edit is not disabled: get control status of each field
                    fieldControlUserData = get(menuCtrl, 'UserData');
                    set(menuCtrl, 'Enable', fieldControlUserData.ctrlStatus.enable);
                    set(menuCtrl, 'Visible', 'on');
                end
            end
        end
    end
end

function activateEdit(hObject, dummy)  %#ok<INUSD>

if strcmp(get(hObject,'Enable'),'inactive') 
    set(hObject,'Enable','on');
    % set complete string
    userData = get(hObject, 'UserData');
    set(hObject, 'string', userData.ctrlStatus.string);
    % set focus
    uicontrol(hObject);
end 

% end % end of function  TLSetCtrlsStatus
